from app import db
from datetime import datetime


class NumberTask(db.Model):
    __tablename__ = 'number_task'

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    task_name = db.Column(db.String(255), nullable=True, comment='任务名称')
    execute_time = db.Column(db.DateTime, default=datetime.now, comment='执行时间')
    country = db.Column(db.String(255), nullable=True, comment='归属国家')
    status = db.Column(db.Integer, default=0, comment='-1 任务执行失败；0待执行 1任务生成号码中 2号码生成完成 3号码筛选中 4号码筛选完成')
    type = db.Column(db.Integer, default=0, comment='任务类型 0：手机号随机筛选 1：手机号号码段筛选')
    step = db.Column(db.Integer, default=0, comment='0 只生成号码；1 过滤号码')
    total_num = db.Column(db.BigInteger, default=0, comment='总数量')
    success_num = db.Column(db.BigInteger, default=0, comment='有效数量')
    number_g_path = db.Column(db.String(255), nullable=True, comment='手机号生成地址')
    number_s_path = db.Column(db.String(255), nullable=True, comment='手机号筛选后的地址')
    task_uuid = db.Column(db.String(255), nullable=True, comment='任务唯一编码')
    create_time = db.Column(db.DateTime, default=datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, comment='变更时间')

    # 定义索引
    __table_args__ = (
        db.Index('idx_name', 'task_name', mysql_length=255, mysql_using='BTREE'),
    )

    def to_json(self):
        return {
            'id': self.id,
            'taskName': self.task_name,
            'executeTime': self.execute_time,
            'country': self.country,
            'status': self.status,
            'type': self.type,
            'step': self.step,
            'totalNum': self.total_num,
            'successNum': self.success_num,
            'numberGPath': self.number_g_path,
            'numberSPath': self.number_s_path,
            'taskUUID': self.task_uuid,
            'createTime': self.create_time,
            'updateTime': self.update_time,
        }
